<?php

/**
 * Description of authuser
 *
 * @author user
 */
class Authuser {

    public $user;

    public function __construct() {
        if (isset($_SESSION['username'])) {
            $this->user = new ORM('user');
            $username = $_SESSION["username"];
            $this->user->where("`username` = '$username'")->find();
        }
    }

    public function is_login() {
        return ($this->user != null) ? TRUE : FALSE;
    }

    public function login($username, $password) {
        $user = new ORM('user');
        $user->where("`username`='$username'")->find();

        if ($user->id != null && $user->role != 'blocked') {
            if (substr(md5($password), 0, 256) == $user->password) {
                $_SESSION["username"] = $username;
                $this->user = & $user;
            }
        } else {
            return FALSE;
        }

        return TRUE;
    }

    public function logout() {
        unset($_SESSION['username']);
        unset($this->user);
    }

}

?>
